package com.yunshan.welfare.service.impl;

import com.yunshan.common.core.domain.entity.SysRole;
import com.yunshan.common.utils.DateUtils;
import com.yunshan.common.utils.SecurityUtils;
import com.yunshan.common.utils.StringUtils;
import com.yunshan.mp.domain.MpServeType;
import com.yunshan.mp.service.IMpServeTypeService;
import com.yunshan.welfare.domain.WelfareCoupon;
import com.yunshan.welfare.domain.WelfareCouponRecord;
import com.yunshan.welfare.mapper.WelfareCouponMapper;
import com.yunshan.welfare.service.IWelfareCouponRecordService;
import com.yunshan.welfare.service.IWelfareCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券管理Service业务层处理
 *
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class WelfareCouponServiceImpl implements IWelfareCouponService
{
    @Autowired
    private WelfareCouponMapper welfareCouponMapper;
    @Autowired
    private IWelfareCouponRecordService welfareCouponRecordService;
    @Autowired
    private IMpServeTypeService mpServeTypeService;

    /**
     * 查询优惠券管理
     *
     * @param id 优惠券管理主键
     * @return 优惠券管理
     */
    @Override
    public WelfareCoupon selectWelfareCouponById(Long id)
    {
        return welfareCouponMapper.selectWelfareCouponById(id);
    }

    /**
     * 查询优惠券管理列表
     *
     * @param welfareCoupon 优惠券管理
     * @return 优惠券管理
     */
    @Override
    public List<WelfareCoupon> selectWelfareCouponList(WelfareCoupon welfareCoupon)
    {
        List<WelfareCoupon> welfareCoupons = new ArrayList<>();

        //部门角色只展示本部门优惠券
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles.stream().anyMatch(role -> "bm".equals(role.getRoleKey()))){
            MpServeType mpServeType = mpServeTypeService.selectMpServeTypeByDeptId(SecurityUtils.getDeptId());

            //如果筛选了其他部门则返回空
            if (StringUtils.isNotEmpty(welfareCoupon.getServeType()) && !welfareCoupon.getServeType().equals(mpServeType.getType())){
                return welfareCoupons;
            }

            welfareCoupon.setServeType(mpServeType.getType());
        }

        welfareCoupons = welfareCouponMapper.selectWelfareCouponList(welfareCoupon);
        for (WelfareCoupon coupon : welfareCoupons) {
            WelfareCouponRecord welfareCouponRecord = new WelfareCouponRecord();
            welfareCouponRecord.setCouponId(coupon.getId());
            welfareCouponRecord.setStatus(0);
            coupon.setGiveNum(welfareCouponRecordService.selectWelfareCouponRecordList(welfareCouponRecord).size());
            welfareCouponRecord.setStatus(1);
            coupon.setVerificationNum(welfareCouponRecordService.selectWelfareCouponRecordList(welfareCouponRecord).size());
        }
        return welfareCoupons;
    }

    /**
     * 新增优惠券管理
     *
     * @param welfareCoupon 优惠券管理
     * @return 结果
     */
    @Override
    public int insertWelfareCoupon(WelfareCoupon welfareCoupon)
    {
        welfareCoupon.setCreateTime(DateUtils.getNowDate());
        return welfareCouponMapper.insertWelfareCoupon(welfareCoupon);
    }

    /**
     * 修改优惠券管理
     *
     * @param welfareCoupon 优惠券管理
     * @return 结果
     */
    @Override
    public int updateWelfareCoupon(WelfareCoupon welfareCoupon)
    {
        welfareCoupon.setUpdateTime(DateUtils.getNowDate());
        return welfareCouponMapper.updateWelfareCoupon(welfareCoupon);
    }

    /**
     * 批量删除优惠券管理
     *
     * @param ids 需要删除的优惠券管理主键
     * @return 结果
     */
    @Override
    public int deleteWelfareCouponByIds(Long[] ids)
    {
        return welfareCouponMapper.deleteWelfareCouponByIds(ids);
    }

    /**
     * 删除优惠券管理信息
     *
     * @param id 优惠券管理主键
     * @return 结果
     */
    @Override
    public int deleteWelfareCouponById(Long id)
    {
        return welfareCouponMapper.deleteWelfareCouponById(id);
    }
}
